package com.bsg.upm.check;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.bsg.upm.constant.DictTypeConstants;
import com.bsg.upm.constant.Messages;
import com.bsg.upm.entity.PrivilegeEntity;

/**
 * 服务用户业务处理检查
 * 
 * @author HCK
 *
 */
@Service
public class ServUserCheck extends BaseCheck {

	/**
	 * 用户新增时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkSave(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkSaveNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkSaveLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 用户编辑时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkUpdate(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkUpdateNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkUpdateLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 新增时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	@SuppressWarnings("unchecked")
	private CheckResult checkSaveNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 服务编码非空检查
		String servId = (String) paramMap.get("servId");
		if (StringUtils.isBlank(servId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "服务编码"));
			return chkRS;
		}

		// 模式非空检查
		String model = (String) paramMap.get("model");
		if (StringUtils.isBlank(model)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "模式"));
			return chkRS;
		}

		// 用户名非空和长度检查
		String username = (String) paramMap.get("username");
		if (StringUtils.isBlank(username)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "用户名"));
			return chkRS;
		}
		if (StringUtils.trim(username).length() > 32) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "用户名", 32));
			return chkRS;
		}

		// 密码非空和长度检查
		String password = (String) paramMap.get("password");
		if (StringUtils.isBlank(password)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "密码"));
			return chkRS;
		}
		if (StringUtils.trim(password).length() > 32) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "密码", 32));
			return chkRS;
		}

		// 库名和权限非空检查
		Map<String, Object> dbPrivileges = (Map<String, Object>) paramMap.get("dbPrivileges");
		for (Map.Entry<String, Object> entry : dbPrivileges.entrySet()) {
			if (StringUtils.isBlank(entry.getKey())) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "库名"));
				return chkRS;
			}
			List<String> privileges = (List<String>) entry.getValue();
			if (privileges == null || privileges.size() == 0) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "权限"));
				return chkRS;
			}
		}
		return chkRS;
	}

	/**
	 * 新增时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	@SuppressWarnings("unchecked")
	private CheckResult checkSaveLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 模式存在性检查
		String model = (String) paramMap.get("model");
		if (dictTypeCache.getDictFromCache(DictTypeConstants.USER_MODEL, model) == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "模式"));
			return chkRS;
		}

		// 权限存在性检查
		Map<String, Object> dbPrivileges = (Map<String, Object>) paramMap.get("dbPrivileges");
		List<PrivilegeEntity> privileges = privilegeDao.list(null);
		for (Map.Entry<String, Object> entry : dbPrivileges.entrySet()) {
			List<String> privilegesStr = (List<String>) entry.getValue();
			for (String privilegeStr : privilegesStr) {
				PrivilegeEntity result = findPrivilege(privileges, privilegeStr);
				if (result == null) {
					chkRS.setStatus(HttpStatus.SC_GONE);
					chkRS.setErrorMsg(privilegeStr + "权限不存在");
					return chkRS;
				}

				if (!result.getEnabled()) {
					chkRS.setStatus(HttpStatus.SC_GONE);
					chkRS.setErrorMsg(privilegeStr + "权限已被禁用");
					return chkRS;
				}
			}
		}
		return chkRS;
	}

	/**
	 * 编辑时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	@SuppressWarnings("unchecked")
	private CheckResult checkUpdateNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 服务编码非空检查
		String servId = (String) paramMap.get("servId");
		if (StringUtils.isBlank(servId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "服务编码"));
			return chkRS;
		}

		if (paramMap.containsKey("model")) {
			// 模式非空检查
			String model = (String) paramMap.get("model");
			if (StringUtils.isBlank(model)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "模式"));
				return chkRS;
			}
		}

		if (paramMap.containsKey("password")) {
			// 密码非空和长度检查
			String password = (String) paramMap.get("password");
			if (StringUtils.isBlank(password)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "密码"));
				return chkRS;
			}
			if (StringUtils.trim(password).length() > 32) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "密码", 32));
				return chkRS;
			}
		}

		if (paramMap.containsKey("dbPrivileges")) {
			// 库名和权限非空检查
			Map<String, Object> dbPrivileges = (Map<String, Object>) paramMap.get("dbPrivileges");
			for (Map.Entry<String, Object> entry : dbPrivileges.entrySet()) {
				if (StringUtils.isBlank(entry.getKey())) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "库名"));
					return chkRS;
				}
				List<String> privileges = (List<String>) entry.getValue();
				if (privileges == null || privileges.size() == 0) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "权限"));
					return chkRS;
				}
			}
		}
		return chkRS;
	}

	/**
	 * 新增时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	@SuppressWarnings("unchecked")
	private CheckResult checkUpdateLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		if (paramMap.containsKey("model")) {
			// 模式存在性检查
			String model = (String) paramMap.get("model");
			if (dictTypeCache.getDictFromCache(DictTypeConstants.USER_MODEL, model) == null) {
				chkRS.setStatus(HttpStatus.SC_GONE);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "模式"));
				return chkRS;
			}
		}

		if (paramMap.containsKey("dbPrivileges")) {
			// 权限存在性检查
			Map<String, Object> dbPrivileges = (Map<String, Object>) paramMap.get("dbPrivileges");
			List<PrivilegeEntity> privileges = privilegeDao.list(null);
			for (Map.Entry<String, Object> entry : dbPrivileges.entrySet()) {
				List<String> privilegesStr = (List<String>) entry.getValue();
				for (String privilegeStr : privilegesStr) {
					PrivilegeEntity result = findPrivilege(privileges, privilegeStr);
					if (result == null) {
						chkRS.setStatus(HttpStatus.SC_GONE);
						chkRS.setErrorMsg(privilegeStr + "权限不存在");
						return chkRS;
					}

					if (!result.getEnabled()) {
						chkRS.setStatus(HttpStatus.SC_GONE);
						chkRS.setErrorMsg(privilegeStr + "权限已被禁用");
						return chkRS;
					}
				}
			}
		}
		return chkRS;
	}

	private PrivilegeEntity findPrivilege(List<PrivilegeEntity> privileges, String privilegeStr) {
		for (PrivilegeEntity privilege : privileges) {
			if (privilege.getCode().equals(privilegeStr)) {
				return privilege;
			}
		}
		return null;
	}
}
